class Solution:
    def romanToInt(self, s: str) -> int:
        result=0
        i=0
        while i<len(s):
            print(s[i])
            if s[i]=='M':
                result+=1000
                i+=1
            elif s[i]=='C':
                if i+1<len(s):
                    if s[i+1]=='M':
                        result+=900
                        i+=2
                    elif s[i+1]=='D':
                        result+=400
                        i+=2
                    else:
                        result+=100
                        while i+1<len(s) and s[i+1]=="C":
                            result+=100
                            i+=1
                        i+=1
                else:
                    result+=100
                    return result
            elif s[i]=='D':
                result+=500
                i+=1
            elif s[i]=='X':
                if i+1<len(s):
                    if s[i+1]=='C':
                        result+=90
                        i+=2
                    elif s[i+1]=='L':
                        result+=40
                        i+=2
                    else:
                        result+=10
                        while i+1<len(s) and s[i+1]=="X":
                            result+=10
                            i+=1
                        i+=1
                else:
                    result+=10
                    return result
            elif s[i]=="L":
                result+=50
                i+=1
            elif s[i]=='I':
                if i+1<len(s):
                    if s[i+1]=='V':
                        result+=4
                        i+=2
                    elif s[i+1]=="X":
                        result+=9
                        i+=2
                    else:
                        result+=1
                        while i+1<len(s) and s[i+1]=='I':
                            result+=1
                            i+=1
                        i+=1
                else:
                    result+=1
                    return result
            elif s[i]=='V':
                result+=5
                i+=1
        return result
s=Solution()
print(s.romanToInt("DCXXI"))


'''
执行用时：
40 ms
, 在所有 Python3 提交中击败了
99.55%
的用户
内存消耗：
14.8 MB
, 在所有 Python3 提交中击败了
5.27%
的用户
'''